package com.sdy.healthcode.web.controller;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.healthcode.biz.constants.ParamConstants;
import com.sdy.healthcode.biz.service.ExpertsAppointmentService;
import com.sdy.healthcode.biz.service.HolidayService;
import com.sdy.healthcode.biz.service.SysParamService;
import com.sdy.healthcode.web.vo.AppointmentCancelSettingsVO;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/appointment/settings")
@Api(tags = "预约配置")
public class AppointmentSettingsController extends BaseController {
    @Autowired
    private ExpertsAppointmentService expertsAppointmentService;
    @Autowired
    private SysParamService sysParamService;
    @Autowired
    private HolidayService holidayService;
    
    @PostMapping("/updateSettings")
    @ApiOperation(value = "更新配置")
    public Response<Object> updateSettings(@RequestBody AppointmentCancelSettingsVO appointmentCancelSettingsVO) throws BizException {
        Assert.isTrue(appointmentCancelSettingsVO.getExpertsAppointmentBlackListCount() <= 0, "次数不能小于1次");
        Assert.isTrue(appointmentCancelSettingsVO.getExpertsAppointmentBlackListDays() < 0, "天数不能小于0天");
        expertsAppointmentService.updateSettings(
                appointmentCancelSettingsVO.getExpertsAppointmentBlackListCount(),
                appointmentCancelSettingsVO.getExpertsAppointmentBlackListDays(),
                appointmentCancelSettingsVO.getScheduleForHoliday(),
                appointmentCancelSettingsVO.getHolidayConfig());
        return Response.success();
    }

    @GetMapping("/getSettings")
    @ApiOperation(value = "获取配置")
    @ApiImplicitParam(value = "年(不传则为本年)", name = "year")
    public Response<AppointmentCancelSettingsVO> getSettings(Integer year) {
        AppointmentCancelSettingsVO vo = new AppointmentCancelSettingsVO()
                .setExpertsAppointmentBlackListCount(Integer.valueOf(sysParamService.getOrDefault(ParamConstants.EXPERTS_APPOINTMENT_BLACK_LIST_COUNT, "3")))
                .setExpertsAppointmentBlackListDays(Integer.valueOf(sysParamService.getOrDefault(ParamConstants.EXPERTS_APPOINTMENT_BLACK_LIST_DAYS, "30")))
                .setScheduleForHoliday(Boolean.valueOf(sysParamService.getOrDefault(ParamConstants.SCHEDULE_FOR_HOLIDAY, "false")))
                .setHolidayConfig(holidayService.getHolidaysForYear(year));
        return Response.success(vo);
    }
}
